<?php
class select extends validatableinput {
		
	public $values = array();
	public $selected = array();
	public $disabled = 0;
	
	public function __construct(){
		$this->validAttributes[] = 'size';
		$this->validAttributes[] = 'multiple';
		$this->validAttributes[] = 'onblur';
		$this->validAttributes[] = 'onchange';	
		$this->attributes['size'] = 1;
		$this->tag = 'select';	
	}

	public function __toString(){
		//opening tag
		$this->html = '<select';

		foreach ( $this->attributes as $key => $value){
			if ( ! empty($value) || strlen($value)){
				$this->html .= ' ' . $key . '="' . $value . '"';
			}
		}
		
		
		if ( isset($this->attributes['multiple']) && ($this->attributes['multiple'] == true)){
			$this->html .= ' multiple';
		}
		
		
		$this->html .= '>' . "\n";

		if ( isset($this->defaultDisplay)){
			$this->html .= '<option value="">' . $this->defaultDisplay . '</option>' . "\n";
		}

		//options
		foreach ( $this->values as $value => $displayvalue){
			$selected = '';
			if ( is_array($this->selected) && count($this->selected)){
				if ( in_array($value, $this->selected)) {
					$selected = ' selected';
				}
			}
			$this->html .= '<option value="' . $value . '"'  . $selected . '>' . $displayvalue . '</option>' . "\n";
		}

		//closing tag
		$this->html .= "</select>\n";
		return $this->html;
	}
	
	public function setSelected($value){
		$this->selected[] = $value;
	}

	public function setDefaultDisplay($string){
		$this->defaultDisplay = $string;
	}

	public function setValues($values){
		if ( is_array($values)){
			$this->values = $values;
		}
	}

	public function addValue($key, $value = ''){
		$this->values[$key] = (strlen($value))? $value:$key;
	}
	
	public function setMultiple(){
		$this->attributes['multiple'] = 'true';
	}
	
	public function setSingle(){
		if ( isset($this->attributes['multiple'])){
			unset($this->attributes['multiple']);
		}
	}
	
	public function setSize($size){
		$this->attributes['size'] = $size;
	}

}
?>